package main

import (
	"fmt"
	"sync"
)

// sync.Mutex 互斥锁
type SafeCounter struct {
	mu sync.Mutex
	v  map[string]uint
}

func (c *SafeCounter) Inc(key string) {
	c.mu.Lock()

	defer c.mu.Unlock()
	c.v[key]++
}

func (c *SafeCounter) GetValue(key string) uint {
	c.mu.Lock()

	defer c.mu.Unlock()
	return c.v[key]
}

func main() {
	param := SafeCounter{v: make(map[string]uint)}
	param.Inc("test")
	param.Inc("test")
	fmt.Println(param.GetValue("test"))
}
